package org.sanguine.study.hometasks.first;

import java.util.Arrays;

public class SquareMatrixOfDouble extends MatrixOfDouble {

	public SquareMatrixOfDouble(int numberOfRows) {
		super(numberOfRows, numberOfRows);
	}

	public Double getDeterminant() {
		Double det = null;
		if (matrix != null) {
			det = 1.0;
			
			double[][] workingCopy = new double[matrix.length][];
			for (int i = 0; i < matrix.length; i++) workingCopy[i] = Arrays.copyOf(matrix[i], matrix[i].length);
						
			for(int i = 0; i < matrix.length; i++) {
				for(int j = i + 1; j < matrix.length; j++) {
					if (workingCopy[i][i] == 0.0) return 0.0;
					
					double b = workingCopy[j][i] / workingCopy[i][i];
					for(int k = i; k < matrix.length; k++)
						workingCopy[j][k] = workingCopy[j][k] - workingCopy[i][k] * b;
				}
				det *= workingCopy[i][i];
			}
		}
		return det;
	}

}
